/**
 * @Title: HistoryRecordController.java
 * @Package com.qding.park.api.web.service.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2011 
 * Company:千丁互联
 * 
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:51:06
 * @version V1.0
 */

package com.qding.park.manager.rest.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.common.core.exceptions.system.SystemUnKnown;
import com.qding.park.common.core.validate.RchlValidateBean;
import com.qding.park.common.core.validate.RchlValidateTools;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.park.manager.facade.core.service.DemoFacade;
import com.qding.park.manager.facade.core.service.common.CarCommonFacade;
import com.qding.park.manager.facade.core.service.common.HisCommonFacade;
import com.qding.park.manager.facade.core.service.common.InoutCommonFacade;

/**
 * @ClassName: HistoryRecordController
 * @Description: 历史记录Controller
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:51:06
 *
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/historyRecord")
public class HistoryRecordController extends WebBaseController {
	private static final Log _log = LogFactory.getLog(HistoryRecordController.class);

	@Autowired
	private CarCommonFacade carCommonFacade;

	@Autowired
    private HisCommonFacade   hisCommonFacade;
	
	/**
	  * @Fields inoutCheckFacade : TODO（出入场记）
	  */
	@Autowired
	private InoutCommonFacade  inoutCommonFacade;
	
	@Autowired
	private DemoFacade  demoFacade;
	/**
	 * @Description (查询历史记录)
	 * @param userId
	 *            北京用户id
	 * @param recordType
	 *            记录类型
	 * @param jumpPage
	 *            页码
	 * @param pageLength
	 *            页长
	 * @return
	 */
	@RequestMapping("/recordList")
	public AnswerMsg recordList(String userId, String recordType, String jumpPage, String pageLength) {
			 
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("userId", userId);
			vb.getRequiredValidate().put("recordType", recordType);
			vb.getRequiredValidate().put("jumpPage", jumpPage);
			vb.getRequiredValidate().put("pageLength", pageLength);
			
			vb.getIntValidate().put("recordType", recordType);
			vb.getIntValidate().put("jumpPage", jumpPage);
			vb.getIntValidate().put("pageLength", pageLength);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult = null;
			if(this.isDemoRequest(userId)){
				//演示请求
				parkDataResult=demoFacade.recordList(userId, Integer.valueOf(recordType),
						Integer.valueOf(jumpPage), Integer.valueOf(pageLength));
			}else{
				 parkDataResult = hisCommonFacade.recordList(userId, Integer.valueOf(recordType),
						Integer.valueOf(jumpPage), Integer.valueOf(pageLength));
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + ";" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch(BaseException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e,
					ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name, "未知Exception",
							"userId=" + userId + "recordType=" + recordType + "jumpPage=" + jumpPage + "pageLength="
									+ pageLength));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}

	}

	@RequestMapping("/recordInfo")
	public AnswerMsg recordInfo(String orderId, String recordType) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("orderId", orderId);
			vb.getRequiredValidate().put("recordType", recordType);

			vb.getIntValidate().put("recordType", recordType);
			RchlValidateTools.validate(vb);
			/**************/
			
			ParkDataResult parkDataResult = null;
			if(this.isDemoRequestByOrder(orderId)){
				//演示请求
				parkDataResult=demoFacade.recordInfo(orderId,Integer.valueOf(recordType));
			}else{
				parkDataResult = hisCommonFacade.recordInfo(orderId,
						Integer.valueOf(recordType));
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + ";" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch(BaseException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}

	}
	
	@RequestMapping("/recordDel")
	public AnswerMsg recordDel(String orderId, String recordType) {
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("orderId", orderId);
			vb.getRequiredValidate().put("recordType", recordType);

			vb.getIntValidate().put("recordType", recordType);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult = null;
			if(this.isDemoRequestByOrder(orderId)){
				//演示请求
				return AnswerMsgFactoryImpl.createAnswerMsg(ParkDataResult.createEmpty().getData());
			}else{
				parkDataResult = hisCommonFacade.recordDel(orderId,
						Integer.valueOf(recordType));
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + ";" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch(BaseException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}

	}
	
	
	

	@RequestMapping("/inviteList")
	public AnswerMsg inviteList(String userId, String jumpPage, String pageLength) {
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("userId", userId);
			vb.getRequiredValidate().put("jumpPage", jumpPage);
			vb.getRequiredValidate().put("pageLength", pageLength);

			vb.getIntValidate().put("jumpPage", jumpPage);
			vb.getIntValidate().put("pageLength", pageLength);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult =null;
			if(this.isDemoRequest(userId)){
				//演示请求
				parkDataResult=this.demoFacade.inviteList(userId,Integer.valueOf(jumpPage),Integer.valueOf(pageLength));
			}else{
				parkDataResult = hisCommonFacade.inviteList(userId, Integer.valueOf(jumpPage),
						Integer.valueOf(pageLength));
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + ";" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch(BaseException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}
	
	
	/**
	 * @param orderId  订单号
	 * @return
	 * 删除邀请记录
	 */
	@RequestMapping("/inviteDel")
	public AnswerMsg inviteDel(String orderId) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("orderId", orderId);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult = null;
			if(this.isDemoRequestByOrder(orderId)){
				//演示请求
				return  AnswerMsgFactoryImpl.createAnswerMsg(ParkDataResult.createEmpty().getData());
			}else{
				parkDataResult = hisCommonFacade.inviteDel(orderId);
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + ";" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch(BaseException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}
	
	@RequestMapping("/lockCarList")
	public AnswerMsg lockCarList(String userId, String jumpPage, String pageLength) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("userId", userId);
			vb.getRequiredValidate().put("jumpPage", jumpPage);
			vb.getRequiredValidate().put("pageLength", pageLength);

			vb.getIntValidate().put("jumpPage", jumpPage);
			vb.getIntValidate().put("pageLength", pageLength);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult =null;
			if(this.isDemoRequest(userId)){
				//演示数据
				parkDataResult=this.demoFacade.lockedCarList(userId, Integer.valueOf(jumpPage),
						Integer.valueOf(pageLength));
			}else{
				parkDataResult = hisCommonFacade.lockCarList(userId, Integer.valueOf(jumpPage),
						Integer.valueOf(pageLength));
			}
		
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + ";" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch(BaseException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}
	
	
	@RequestMapping("/lockCarDel")
	public AnswerMsg lockCarDel(String orderId) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("orderId", orderId);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult =null;
			if(this.isDemoRequestByOrder(orderId)){
				//演示请求
				return AnswerMsgFactoryImpl.createAnswerMsg(ParkDataResult.createEmpty().getData());
			}else{
				parkDataResult = hisCommonFacade.lockCarDel(orderId);
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + ";" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch(BaseException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}
	
	
	@RequestMapping("/passList")
	public AnswerMsg passList(String userId, String jumpPage, String pageLength) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("userId", userId);
			vb.getRequiredValidate().put("jumpPage", jumpPage);
			vb.getRequiredValidate().put("pageLength", pageLength);

			vb.getIntValidate().put("jumpPage", jumpPage);
			vb.getIntValidate().put("pageLength", pageLength);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult =null;
			if(this.isDemoRequest(userId)){
				//演示请求
				parkDataResult=demoFacade.passList(userId, Integer.valueOf(jumpPage),Integer.valueOf(pageLength));
			}else{
				parkDataResult = inoutCommonFacade.recordList(userId, Integer.valueOf(jumpPage),
						Integer.valueOf(pageLength));
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + ";" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch(BaseException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}
	
	@RequestMapping("/passDel")
	public AnswerMsg passDel(String recordId) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("recordId", recordId);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult =null;
			if(this.isDemoRequestByOrder(recordId)){
				//演示请求
				return  AnswerMsgFactoryImpl.createAnswerMsg(ParkDataResult.createEmpty().getData());
			}else{
				parkDataResult = inoutCommonFacade.passDel(Integer.valueOf(recordId));
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + ";" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch(BaseException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}

}
